package com.xxx.modules.controller;;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxx.modules.mapper.UserMapper;
import com.xxx.modules.service.UserService;
import com.xxx.modules.entity.User;
import com.xxx.modules.utils.Md5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageInfo;

import com.xxx.modules.utils.Result;
import com.xxx.modules.utils.ResultUtil;


/**
 * user
 *
 * @author 北易航
 * @since 1.0.0 2022-12-19
 */
@RestController
@RequestMapping("/common/user")
@Api(tags="user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
    *  查询所有user
    * @param pageNum
    * @param pageSize
    * @param user
    * @return
    */

    @GetMapping("/getUserInfo")
    @ApiOperation("获取所有user")
    public Result getUserInfo(User user, @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                      @RequestParam(value="pageSize",defaultValue = "10") Integer pageSize){
        PageInfo<User> data = userService.selectUserInfo(user,pageNum,pageSize);
        return ResultUtil.success(1,"成功",data);
    }

    /**
    * 保存user
    * @param user
    * @return
    */
    @PostMapping("/saveUserInfo")
    @ApiOperation("保存user")
    public Result saveUserInfo(@RequestBody User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",user.getPhone());
        User user1 = userMapper.selectOne(wrapper);
        if (user1 !=null){
            return ResultUtil.error(-1,"手机号重复");
        }
        QueryWrapper<User> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("id_card",user.getIdCard());
        User user2 = userMapper.selectOne(wrapper2);
        if (user2 !=null){
            return ResultUtil.error(-1,"身份证号重复");
        }
        QueryWrapper<User> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("user_name",user.getUserName());
        User user3 = userMapper.selectOne(wrapper3);
        if (user3 !=null){
            return ResultUtil.error(-1,"用户名重复");
        }
        userService.saveUserInfo(user);
        return ResultUtil.success(1,"成功",null);
    }


    /**
     * 更新user
     * @param user
     * @return
     */

    @PutMapping("/updateUserInfo")
    @ApiOperation("更新")
    public Result updateUserInfo(@RequestBody User user ){
//        String password = request.getString("password");
//        String md5Password = Md5Util.MD5(password);
//        User user = userService.login(md5Password);
        userService.updateUserInfo(user);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id删除user
     * @param id
     * @return
     */
    @DeleteMapping("/delUserInfo")
    public Result delUserInfo(Integer id){
        userService.delUserInfo(id);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id集合删除user
     * @param idList
     * @return
     */
    @DeleteMapping("/delBatchUserInfo")
    public Result delBatchUserInfo(String idList){
        userService.delBatchUserInfo(idList);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 登录接口(根据用户名和密码)
     * @param userName
     * @param password
     * @return
     */

    @PostMapping("/login")
    @ApiOperation("用户登录接口")
    public Result login(String userName, String password,Integer userType){
        String md5Password = Md5Util.MD5(password);
        User user = userService.login(userName,md5Password,userType);
        if (user == null){
            return ResultUtil.error(-1,"登录失败,用户名或者密码错误");
        }else {
            return ResultUtil.success(1,"登录成功",user);
        }
    }

    /**
     * 注册接口(根据用户名和密码)
     * @param userName
     * @param password
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("用户注册接口")
    public Result register(@RequestBody JSONObject request){
        String password = request.getString("password");
        String userName = request.getString("userName");
        Integer userType = request.getInteger("userType");
        String md5Password = Md5Util.MD5(password);
        User user = userService.register(userName,md5Password,userType);
        if (user == null){
            return ResultUtil.error(-1,"注册失败");
        }else {
            return ResultUtil.success(1,"注册成功",user);
        }
    }

    @Autowired
    private UserMapper userMapper;


    @PutMapping("/bindWork")
    public Result bindWork(@RequestBody User user){
        try {
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("work_id",user.getWorkId());
//            User user1 = userMapper.selectOne(wrapper);
//            if (user1 !=null){
//              return ResultUtil.error(-1,"该护工已经绑定其他用户了");
//            }
            userMapper.updateById(user);
            return ResultUtil.success(1,"正常",null);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"异常");
        }
    }

    @PostMapping("/updatePassword")
    @ApiOperation("修改密码接口")
    public Result updatePassword(@RequestBody JSONObject request){
        String userName = request.getString("userName");
        String oldPassword = request.getString("oldPassword");
        String newPassword = request.getString("newPassword");
        User user = userService.updatePassword(userName,oldPassword,newPassword);
        if (user == null){
            return ResultUtil.error(-1,"修改失败");
        }else {
            return ResultUtil.success(1,"修改成功",user);
        }
    }


}